package pl.edu.agh.iosr.gamblingzone.userpanel.action;

import static org.jboss.seam.ScopeType.SESSION;

import java.util.LinkedList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.MoneyVoucherDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.MoneyVoucherDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.MoneyVoucher;
import pl.edu.agh.iosr.gamblingzone.model.User;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateful
@Scope(SESSION)
@Name("userVouchers")
@Restrict("#{identity.loggedIn}")
public class MoneyVoucherHistoryViewAction implements MoneyVoucherHistoryView {
	
	@In
	private User user;

	@PersistenceContext
	private EntityManager em;
	
	@In(required=true)
	DAOFactory dao;
	
	@Logger
	Log log;
	
	private MoneyVoucherDAO voucherDao;
	
	@DataModel
	private List<MoneyVoucher> vouchers;
	private List<MoneyVoucher> tmpVouchers;
	
	@Create
	public void init() {
		
		voucherDao = dao.getVoucherDAO();
		
		vouchers = new LinkedList<MoneyVoucher>();
		tmpVouchers = new LinkedList<MoneyVoucher>();
	}
	
	public List<MoneyVoucher> findMoneyVouchers() {
		int pages = voucherDao.getPageCount();
		tmpVouchers.clear();
		vouchers.clear();
		
		for(int i = 0; i <= pages; i++) {
			tmpVouchers.addAll(voucherDao.findAll(user, i));
		}
		
		for (MoneyVoucher voucher : tmpVouchers) {
			if(!voucher.isActive()) {
				vouchers.add(voucher);
			}
		}
		
		return null;
	}
	
	@Remove
	public void destroy() {
	}
}
